package com.etime.shycourse.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.pojo.Sysbanner;
import com.etime.shycourse.dao.SysbannerMapper;
import com.etime.shycourse.services.interfaces.SysbannerServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wb
 * @since 2022-01-04
 */
@Service
public class SysbannerServiceImpl implements SysbannerServices {
    @Autowired
    SysbannerMapper mapper;

    /**
     * 1、增加 Sysbanner
     * <p>
     * 默认 bannerstate = 1
     * 默认 bannersort = 3
     * 默认 bannersort = 1 课程
     * <p>
     * 2、修改 Sysbanner根据 bannerid
     *
     * @param sysbanner 用户实体(教师)
     * @return 0失败；1成功
     */
    @Override
    public int insertOrUpdateSysBanner(Sysbanner sysbanner) {
        if (sysbanner.getBannerid () != null) {
            return mapper.updateById (sysbanner);
        } else {
            if (sysbanner.getBannerstate () == null) {
                // 默认 bannerstate = 1
                sysbanner.setBannerstate (1);
            }
            if (sysbanner.getBannersort () == null) {
                // 默认 bannersort = 3
                sysbanner.setBannersort (3);
            }
            if (sysbanner.getBannerlinktype () == null) {
                // 默认 bannersort  1 课程
                sysbanner.setBannerlinktype (3);
            }
            return mapper.insert (sysbanner);
        }
    }

    /**
     * 1、根据bannerid查询一条数据 修改前进行数据渲染
     * 2、查询所有banner(List) 没有条件
     *
     * @param bannerId bannerId    可不传
     * @param pageNum, 第几页       可不传
     * @param pageSize 一页多少数据  可不传
     * @return Page<Sysbanner>
     */
    @Override
    public Page<Sysbanner> selectSysBannerPage(Integer bannerId, Long pageNum, Long pageSize) {

        Page<Sysbanner> page;
        if (pageNum != null & pageSize != null) {
            page = new Page<> (pageNum, pageSize);
        } else {
            page = new Page<> ();
        }
        QueryWrapper<Sysbanner> wrapper = new QueryWrapper<> ();
        if (bannerId != null) {
            // 根据bannerid查询一条数据 修改前进行数据渲染
            wrapper.eq ("bannerid", bannerId);
            return mapper.selectPage (page, wrapper);
        } else {
            // 查询所有banner(List) 没有条件
            // 修改为按照 bannerstate 状态排序
            wrapper.orderByDesc ("bannerstate");
            return mapper.selectPage (page, wrapper);
        }
    }

    /**
     * 按 ID 删除
     *
     * @param bannerId
     * @return int
     */
    @Override
    public int deleteById(Integer bannerId) {
        return mapper.deleteById (bannerId);
    }
}
